package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    // 单页最大加载的数字
    private final static Integer PAGE_SIZE = 10;

    @Resource
    private ApArticleMapper apArticleMapper;
    /**
     * 根据页面加载文章列表
     * @param dto
     * @param loadtype 1为下拉加载更多 2 为上拉加载最新
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult load(ArticleHomeDto dto, Short loadtype) {
        //1. 效验参数
        Integer size = dto.getSize();
        if (size == null || size ==0 ){
            //给他设置一个初始大小
            dto.setSize(PAGE_SIZE);
        }
        //2.频道效验
        if (StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //3.时间效验
        if(dto.getMaxBehotTime() == null){
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null){
            dto.setMinBehotTime(new Date());
        }
        //4.查询数据
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, loadtype);
        //5.返回结果
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        return responseResult;
    }


    @Autowired
    private ApArticleConfigMapper configMapper;

    @Autowired
    private ApArticleContentMapper contentMapper;

    @Autowired
    private ArticleDetailService articleDetailService;

    /**
     * 保存app端相关文章
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) throws Exception{
        //1.检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.将审核过后的文章拷贝给app端的文章表里面 ,,属性拷贝
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);

        apArticle.setPublishTime(new Date());

        //3.判断是否存在id
        if (dto.getId() == null){
            //3.1 如果id不存在 保存文章相关内容 就是保存文章 文章配置 文章内容
            //3.1.1保存文章
            save(apArticle);
            //3.1.2 保存文章配置
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsForward(true);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsComment(true);
            configMapper.insert(apArticleConfig);

            //3.1.3保存文章内容
            ApArticleContent articleContent = new ApArticleContent();
            articleContent.setArticleId(apArticle.getId());
            articleContent.setContent(dto.getContent());
            contentMapper.insert(articleContent);
        }else {
            //3.2 存在id   修改  文章  文章内容
            //3.2.1 修改文章
            updateById(apArticle);
            //3.2.2 修改文章内容
            LambdaQueryWrapper<ApArticleContent> wrapper = new LambdaQueryWrapper<>();

            wrapper.eq(ApArticleContent::getArticleId,dto.getId());
            ApArticleContent apArticleContent = contentMapper.selectOne(wrapper);
            contentMapper.updateById(apArticleContent);
        }



        //创建静态页面
        articleDetailService.createStaticUrl(dto.getContent(),apArticle.getId());



        //4.返回文章的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
